WOLF core
WOLF: A Modular Estimation Framework for Robotics
XXXXXXXXXXXXXXXXXXXXXXXXvoid(0)" frameborder="0" name="MSearchResults" id="MSearchResults">
solver_ceres.h
Go to the documentation of this file.
1// WOLF - Copyright (C) 2020-2026
2// Institut de Robòtica i Informàtica Industrial, CSIC-UPC.
3// Authors: Joan Solà Ortega (jsola@iri.upc.edu) and
4// Joan Vallvé Navarro (jvallve@iri.upc.edu)
5// All rights reserved.
6//
7// This file is part of WOLF: http://www.iri.upc.edu/wolf
8// WOLF is free software: you can redistribute it and/or modify
9// it under the terms of the GNU General Public License version 3
10// as published by the Free Software Foundation.
11//
12// This program is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15// GNU General Public License for more details.
16//
17// You should have received a copy of the GNU General Public License
18// along with this program. If not, see <http://www.gnu.org/licenses/>.
19#pragma once
20
21// Ceres includes
22#include "ceres/jet.h"
23#include "ceres/ceres.h"
24#include "glog/logging.h"
25
26// wolf includes
28#include "yaml-cpp/yaml.h"
29
30namespace ceres
31{
32typedef std::shared_ptr<CostFunction> CostFunctionPtr;
33}
34
35namespace wolf
36{
39
43{
44 protected:
45 std::map<FactorBasePtr, ceres::ResidualBlockId> fac_2_residual_idx_;
46 std::map<FactorBasePtr, ceres::CostFunctionPtr> fac_2_costfunction_;
47
48 // this map is only for handling automatic destruction of ManifoldWrapper objects
49 std::map<StateBlockPtr, ManifoldWrapperPtr> state_blocks_manifold_;
50
51 ceres::Solver::Summary summary_;
52 std::unique_ptr<ceres::Problem> ceres_problem_;
53 std::unique_ptr<ceres::Covariance> covariance_;
54
55 // profiling
56 unsigned int n_iter_acc_;
57 unsigned int n_iter_max_;
58 unsigned int n_convergence_;
59 unsigned int n_interrupted_;
60 unsigned int n_no_convergence_;
61
62 public:
65 SolverCeres(const ProblemPtr& _wolf_problem, const YAML::Node& _params);
66
68
69 ~SolverCeres() override;
70
73 ceres::Solver::Summary getSummary() const;
74
77 std::unique_ptr<ceres::Problem>& getCeresProblem();
78
81
82 bool computeCovariancesDerived(const std::vector<StateBlockPtr>& st_list) override;
83
84 bool converged() const override;
85 bool failed() const override;
86 bool wasStopped() const override;
87 unsigned int iterations() const override;
88 double initialCost() const override;
89 double finalCost() const override;
90 double totalTime() const override;
91
94 ceres::Solver::Options& getSolverOptions();
95
99
100 virtual int numStateBlocksEstimatedDerived() const override;
101
102 virtual int numFactorsDerived() const override;
103 virtual int sizeStateBlocksEstimatedDerived() const override;
104 virtual int sizeFactorsDerived() const override;
105
106 protected:
107 bool checkDerived(std::string prefix = "") const override;
108
109 std::string solveDerived(const ReportVerbosity report_level) override;
110
111 void addFactorDerived(const FactorBasePtr& fac_ptr) override;
112
113 void removeFactorDerived(const FactorBasePtr& fac_ptr) override;
114
115 void addStateBlockDerived(const StateBlockPtr& state_ptr) override;
116
117 void removeStateBlockDerived(const StateBlockPtr& state_ptr) override;
118
119 void updateStateBlockStatusDerived(const StateBlockPtr& state_ptr) override;
120
121 void updateStateBlockLocalParametrizationDerived(const StateBlockPtr& state_ptr) override;
122
125 ceres::CostFunctionPtr createCostFunction(const FactorBasePtr& _fac_ptr);
126
127 bool isFactorRegisteredDerived(const FactorBasePtr& fac_ptr) const override;
128
129 bool isStateBlockRegisteredDerived(const StateBlockPtr& state_ptr) const override;
130
131 bool isStateBlockFixedDerived(const StateBlockPtr& st) const override;
132
133 bool hasThisLocalParametrizationDerived(const StateBlockPtr& st,
134 const LocalParametrizationBasePtr& local_param) const override;
135
136 bool hasLocalParametrizationDerived(const StateBlockPtr& st) const override;
137
138 void printProfilingDerived(std::ostream& _stream) const override;
139
140 // PARAMS
141 ceres::Solver::Options solver_options_;
142};
143
144inline ceres::Solver::Summary SolverCeres::getSummary() const
145{
146 return summary_;
147}
148
149inline std::unique_ptr<ceres::Problem>& SolverCeres::getCeresProblem()
150{
151 return ceres_problem_;
152}
153
154inline ceres::Solver::Options& SolverCeres::getSolverOptions()
155{
156 return solver_options_;
157}
158
159inline bool SolverCeres::isFactorRegisteredDerived(const FactorBasePtr& fac_ptr) const
160{
161 return fac_2_residual_idx_.count(fac_ptr) == 1 and fac_2_costfunction_.count(fac_ptr) == 1;
162}
163
164inline bool SolverCeres::isStateBlockRegisteredDerived(const StateBlockPtr& state_ptr) const
165{
166 if (state_blocks_.count(state_ptr) == 0) return false;
167 return ceres_problem_->HasParameterBlock(getAssociatedMemBlockPtr(state_ptr));
168}
169
170inline bool SolverCeres::isStateBlockFixedDerived(const StateBlockPtr& st) const
171{
172 if (state_blocks_.count(st) == 0) return false;
173 return ceres_problem_->IsParameterBlockConstant(getAssociatedMemBlockPtr(st));
174};
175
176inline bool SolverCeres::hasLocalParametrizationDerived(const StateBlockPtr& st) const
177{
178 return state_blocks_manifold_.count(st) == 1;
179};
180
182{
183 return ceres_problem_->NumResidualBlocks();
184};
185
187{
188 return ceres_problem_->NumResiduals();
189}
190
191} // namespace wolf
Ceres manifold of a wolf local parametrization.
Definition: manifold_wrapper.h:36
Solver manager using Ceres Solver.
Definition: solver_ceres.h:43
bool isFactorRegisteredDerived(const FactorBasePtr &fac_ptr) const override
Returns whether a factor is in the derived solver.
Definition: solver_ceres.h:159
bool hasThisLocalParametrizationDerived(const StateBlockPtr &st, const LocalParametrizationBasePtr &local_param) const override
Returns whether a state block has a given local parametrization in the derived solver.
Definition: solver_ceres.cpp:907
SolverCeres(const ProblemPtr &_wolf_problem, const YAML::Node &_params)
Constructor from the problem and the YAML parameters.
Definition: solver_ceres.cpp:29
virtual int numFactorsDerived() const override
Returns the number of factors in the derived solver.
Definition: solver_ceres.h:181
bool failed() const override
Returns whether the last solve failed.
Definition: solver_ceres.cpp:687
ceres::Solver::Summary getSummary() const
Returns the summary of the last solve.
Definition: solver_ceres.h:144
virtual int sizeFactorsDerived() const override
Returns the total residual size of the factors in the derived solver.
Definition: solver_ceres.h:186
ceres::Solver::Options solver_options_
Ceres solver options.
Definition: solver_ceres.h:141
bool wasStopped() const override
Returns whether the last solve was stopped (interrupted by a problem change)
Definition: solver_ceres.cpp:682
unsigned int n_no_convergence_
Solves terminated without convergence.
Definition: solver_ceres.h:60
bool isStateBlockFixedDerived(const StateBlockPtr &st) const override
Returns whether a state block is fixed in the derived solver.
Definition: solver_ceres.h:170
unsigned int n_interrupted_
Solves terminated by interruption.
Definition: solver_ceres.h:59
bool converged() const override
Returns whether the last solve converged.
Definition: solver_ceres.cpp:677
void printProfilingDerived(std::ostream &_stream) const override
Prints the profiling of the derived solver.
Definition: solver_ceres.cpp:915
double initialCost() const override
Returns the initial cost of the last solve.
Definition: solver_ceres.cpp:698
virtual int sizeStateBlocksEstimatedDerived() const override
Returns the total size of the estimated state blocks in the derived solver.
Definition: solver_ceres.cpp:667
bool isStateBlockRegisteredDerived(const StateBlockPtr &state_ptr) const override
Returns whether a state block is in the derived solver.
Definition: solver_ceres.h:164
unsigned int n_iter_max_
Maximum iterations of a single solve.
Definition: solver_ceres.h:57
unsigned int n_iter_acc_
Accumulated iterations of all solves.
Definition: solver_ceres.h:56
bool checkDerived(std::string prefix="") const override
Checks the consistency of the derived solver.
Definition: solver_ceres.cpp:721
void removeFactorDerived(const FactorBasePtr &fac_ptr) override
Removes a factor from the derived solver.
Definition: solver_ceres.cpp:564
~SolverCeres() override
Definition: solver_ceres.cpp:103
std::map< StateBlockPtr, ManifoldWrapperPtr > state_blocks_manifold_
Ceres manifold of each state block.
Definition: solver_ceres.h:49
bool computeCovariancesDerived(CovarianceBlocksToBeComputed _blocks=CovarianceBlocksToBeComputed::ROBOT_LANDMARKS) override
Computes the covariances of the specified blocks in the derived solver.
Definition: solver_ceres.cpp:152
const Eigen::SparseMatrixd computeHessian() const
Computes the Hessian (JᵀJ) of the problem at the current estimate.
Definition: solver_ceres.cpp:840
std::map< FactorBasePtr, ceres::CostFunctionPtr > fac_2_costfunction_
Ceres cost function of each factor.
Definition: solver_ceres.h:46
double finalCost() const override
Returns the final cost of the last solve.
Definition: solver_ceres.cpp:703
std::unique_ptr< ceres::Problem > ceres_problem_
Ceres problem.
Definition: solver_ceres.h:52
virtual int numStateBlocksEstimatedDerived() const override
Returns the number of estimated state blocks in the derived solver.
Definition: solver_ceres.cpp:657
std::unique_ptr< ceres::Covariance > covariance_
Ceres covariance.
Definition: solver_ceres.h:53
ceres::Solver::Summary summary_
Summary of the last solve.
Definition: solver_ceres.h:51
void updateStateBlockLocalParametrizationDerived(const StateBlockPtr &state_ptr) override
Updates the local parametrization of a state block in the derived solver.
Definition: solver_ceres.cpp:623
std::map< FactorBasePtr, ceres::ResidualBlockId > fac_2_residual_idx_
Ceres residual block of each factor.
Definition: solver_ceres.h:45
bool hasLocalParametrizationDerived(const StateBlockPtr &st) const override
Returns whether a state block has a local parametrization in the derived solver.
Definition: solver_ceres.h:176
void addFactorDerived(const FactorBasePtr &fac_ptr) override
Adds a factor to the derived solver.
Definition: solver_ceres.cpp:532
void updateStateBlockStatusDerived(const StateBlockPtr &state_ptr) override
Updates the fixed/unfixed status of a state block in the derived solver.
Definition: solver_ceres.cpp:612
unsigned int iterations() const override
Returns the number of iterations of the last solve.
Definition: solver_ceres.cpp:692
WOLF_SOLVER_CREATE(SolverCeres)
double totalTime() const override
Returns the total time of the last solve.
Definition: solver_ceres.cpp:708
ceres::Solver::Options & getSolverOptions()
Returns the ceres solver options.
Definition: solver_ceres.h:154
void removeStateBlockDerived(const StateBlockPtr &state_ptr) override
Removes a state block from the derived solver.
Definition: solver_ceres.cpp:601
unsigned int n_convergence_
Solves terminated by convergence.
Definition: solver_ceres.h:58
void addStateBlockDerived(const StateBlockPtr &state_ptr) override
Adds a state block to the derived solver.
Definition: solver_ceres.cpp:577
std::string solveDerived(const ReportVerbosity report_level) override
Solves the problem in the derived solver, returning the report.
Definition: solver_ceres.cpp:123
ceres::CostFunctionPtr createCostFunction(const FactorBasePtr &_fac_ptr)
Creates the ceres cost function of a factor, depending on its Jacobian method.
Definition: solver_ceres.cpp:713
std::unique_ptr< ceres::Problem > & getCeresProblem()
Returns the ceres problem.
Definition: solver_ceres.h:149
Solver manager for WOLF.
Definition: solver_manager.h:84
CovarianceBlocksToBeComputed
Enumeration of covariance blocks to be computed.
Definition: solver_manager.h:89
const double * getAssociatedMemBlockPtr(const StateBlockPtr &state_ptr) const
Returns a pointer to the state vector of a registered state block used by the solver.
Definition: solver_manager.cpp:509
std::map< StateBlockPtr, Eigen::VectorXd > state_blocks_
Registered state blocks and solver states.
Definition: solver_manager.h:297
ReportVerbosity
Enumeration for the verbosity of the solver report.
Definition: solver_manager.h:102
SparseMatrix< double > SparseMatrixd
Sparse matrix of real double type.
Definition: wolf.h:109
Definition: solver_ceres.h:31
std::shared_ptr< CostFunction > CostFunctionPtr
Definition: solver_ceres.h:32
Definition: capture_base.h:23
WOLF_PTR_TYPEDEFS(CaptureLandmarksExternal)